//
//  UILabel+AttributedString.m
//  高仿暴走斗图
//
//  Created by huangsongyao on 16/3/7.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "UILabel+AttributedString.h"

@implementation UILabel (AttributedString)

+ (NSAttributedString *)setLabelRTFPleochroicToColor:(UIColor *)color attributedString:(NSString *)attributedString range:(NSRange)range
{
    if (!color || !attributedString || range.length == 0) {
        return nil;
    }
    NSMutableAttributedString *attributedStr = [self setLabelRTFWithAttribute:NSForegroundColorAttributeName value:color range:range attributedString:attributedString];
    
    return attributedStr;
}


+ (NSAttributedString *)setLabelRTFLineType:(NSUnderlineStyle)lineType attributedString:(NSString *)attributedString range:(NSRange)range
{
    if (!lineType || !attributedString || range.length == 0) {
        return nil;
    }
    NSMutableAttributedString *attributedStr = [self setLabelRTFWithAttribute:NSUnderlineStyleAttributeName value:@(lineType) range:range attributedString:attributedString];
    
    return attributedStr;
}


+ (NSAttributedString *)setLabelRTFFont:(UIFont *)font attributedString:(NSString *)attributedString range:(NSRange)range
{
    if (!font || !attributedString || range.length == 0) {
        return nil;
    }
    NSMutableAttributedString *attributedStr = [self setLabelRTFWithAttribute:NSFontAttributeName value:font range:range attributedString:attributedString];
    
    return attributedStr;
}


+ (NSAttributedString *)setLabelRTFAttributedDic:(NSDictionary *)attributedDic attributedString:(NSString *)attributedString range:(NSRange)range
{
    if (!attributedDic || !attributedString || range.length == 0) {
        return nil;
    }
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:attributedString];
    for (int i = 0; i < attributedDic.allKeys.count; i ++) {
        NSString *key = attributedDic.allKeys[i];
        NSString *value = attributedDic[key];
        [attributedStr addAttribute:key
                              value:value
                              range:range];
    }
    
    
    return attributedStr;
}

+ (NSMutableAttributedString *)setLabelRTFWithAttribute:(NSString *)attribute value:(id)value range:(NSRange)range attributedString:(NSString *)attributedString
{
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:attributedString];
    [attributedStr addAttribute:attribute
                          value:value
                          range:range];
    
    return attributedStr;
}

@end
